import os
import simplejson
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from model import *

class RootHandler(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'map.html')
        self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
        self.response.out.write(template.render(path, {}));

class FeedHandler(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write('{\n')
        self.response.out.write('\t"name":"cheeseburger"\n')
        self.response.out.write('}\n')

class PricesHandler(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'prices.html')
        prices = db.GqlQuery("Select * "
                             "From Price "
                             "Where Ancestor Is :1 "
                             "Order By when Desc",
                             Price.parent_key())

        template_values = {
            'locations': simplejson.
        self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
        self.response.out.write(template.render(path, 
                                                template_values = {}));

    def post(self):
        country = self.request.get('Country')
        currency = self.request.get('Currency')
        itemname = self.request.get('Item')
        cost = self.request.get('Cost')

        locale = Location.get_by_key_name(country)
        if locale:
            item = Item.get_by_key_name(itemname)
            if item:
                price = Price.create(locale, item, int(cost))
                price.put()

        self.redirect('/prices')

class LocationsHandler(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'application/json'
        locations = db.GqlQuery("Select * "
                                "From Location "
                                "Where Ancestor Is :1 ",
                                Location.parent_key())

        self.response.out.write(simplejson.dumps([l.to_dict() for l in locations]))

class ItemsHandler(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'application/json'
        items = db.GqlQuery("Select * "
                                "From Item "
                                "Where Ancestor Is :1 ",
                                Item.parent_key())

        self.response.out.write(simplejson.dumps([i.to_dict() for i in items]))

class PricesFeedHandler(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'application/json'
        prices = db.GqlQuery("Select * "
                                "From Price "
                                "Where Ancestor Is :1 ",
                                Price.parent_key())

        self.response.out.write(simplejson.dumps([p.to_dict() for p in prices]))

